package com.situ.mall.web.controller;

import com.situ.mall.common.service.IMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author yangkx
 * @create 2023-09-19-14:16
 */
@Controller
@RequestMapping("/")
public class LoginController {

    @Autowired
    private IMemberService memberService;

    //打开登陆页面
    @GetMapping("/login")
    public String login(){
        return "login";
    }

    //登陆验证
    @PostMapping("/login")
    public String login(String memberName, String password){
        boolean b = memberService.check(memberName,password);
        //通过验证，重定向到首页
        if (b){
            return "redirect:/";
            //否则继续登录
        }else {
            return "login";
        }
    }

    //登出 删除session
    @GetMapping("/logout")
    public void logout(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //删除session
        request.getSession().invalidate();
        //重定向到主页
        response.sendRedirect("/");

    }
}
